package com.imti.retrofitweatherdemo.logic;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build.VERSION_CODES;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.imti.retrofitweatherdemo.BaseApplication;
import com.imti.retrofitweatherdemo.BuildConfig;
import com.imti.retrofitweatherdemo.fragment.HomeDocModel;
import com.imti.retrofitweatherdemo.logic.NewsData.NewsListItem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import rx.Observable;
import rx.Observable.Operator;
import rx.Observable.Transformer;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class NewsLogic
{

    private static final String TAG = NewsLogic.class.getSimpleName();

    private BaseApplication application;

    private static NewsLogic sInstance;

    private INewsLogic iNewsLogic;

    private Context context;

    public void setiNewsLogic(INewsLogic iNewsLogic)
    {
        this.iNewsLogic = iNewsLogic;
    }

    private NewsLogic(BaseApplication application, Context context)
    {
        this.application = application;
        this.context = context;
    }

    public static NewsLogic getInstance(BaseApplication application, Context context)
    {
        if (null == sInstance)
        {
            sInstance = new NewsLogic(application, context);
        }
        return sInstance;
    }

    public void requestNewsByType(String type)
    {
        List<NewsModelListItem> resultList = new ArrayList<>();

        application.getNewsRequest()
            .requestNewsList(parseTab(type), BuildConfig.NEWS_APPKEY, "1", "20", "1")
            .subscribeOn(Schedulers.io())
            .flatMap(new Func1<NewsData, Observable<NewsListItem>>()
            {
                @Override
                public Observable<NewsListItem> call(NewsData newsData)
                {
                    Observable<NewsListItem> result = Observable.from(newsData.getResult().getData());
                    return result;
                }
            })
            .map(new Func1<NewsListItem, NewsModelListItem>()
            {
                @Override
                public NewsModelListItem call(NewsListItem newsListItem)
                {
                    NewsModelListItem item = new NewsModelListItem();

                    item.setId(newsListItem.getUniquekey());
                    item.setThumbPath(newsListItem.getThumbnail_pic_s());
                    item.setTitle(newsListItem.getTitle());
                    item.setTime("发布时间：" + newsListItem.getDate());

                    if (!TextUtils.isEmpty(item.getId()))
                    {
                        return item;
                    } else
                    {
                        return null;
                    }
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<NewsModelListItem>()
            {
                @Override
                public void onCompleted()
                {
                    iNewsLogic.requestNewsList(resultList);
                }
                @Override
                public void onError(Throwable e)
                {
                    Log.d(TAG, "news list error", e);
                    e.printStackTrace();
                }
                @Override
                public void onNext(NewsModelListItem newsModelListItem)
                {
                    resultList.add(newsModelListItem);
                }
            });
    }

    public void requestDocList()
    {
        List<HomeDocModel> list = new ArrayList<>();

        application.hospitalRequest.requestHomeDocList("22", "1")
            .subscribeOn(Schedulers.io())
            .doOnNext(new Action1<List<HomeDocData>>()
            {
                @RequiresApi(api = VERSION_CODES.N)
                @Override
                public void call(List<HomeDocData> docData)
                {
                    docData.stream().forEachOrdered(new Consumer<HomeDocData>()
                    {
                        @Override
                        public void accept(HomeDocData homeDocData)
                        {
                            HomeDocModel model = new HomeDocModel();

                            model.setName(homeDocData.getName());
                            model.setSex(homeDocData.getSex());
                            model.setPhone(homeDocData.getPhone());
                            model.setDeskName(homeDocData.getDeskName());
                            model.setAddress(homeDocData.getAddress());
                            model.setIntro(homeDocData.getIntro());

                            list.add(model);
                        }
                    });
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<HomeDocData>>()
            {
                @Override
                public void onCompleted()
                {
                    iNewsLogic.requestDocList(list);
                }
                @Override
                public void onError(Throwable e)
                {

                }
                @Override
                public void onNext(List<HomeDocData> docData)
                {

                }
            });
//            .map(new Func1<HomeDocData, HomeDocModel>()
//            {
//                @Override
//                public HomeDocModel call(HomeDocData homeDocData)
//                {
//                    HomeDocModel model = new HomeDocModel();
//
//                    model.setName(homeDocData.getName());
//                    model.setSex(homeDocData.getSex());
//                    model.setPhone(homeDocData.getPhone());
//                    model.setDeskName(homeDocData.getDeskName());
//                    model.setAddress(homeDocData.getAddress());
//                    model.setIntro(homeDocData.getIntro());
//
//                    return model;
//                }
//            })

    }



    //top(推荐,默认)
    //guonei(国内)
    //guoji(国际)
    //yule(娱乐)
    //tiyu(体育)
    //junshi(军事)
    //keji(科技)
    //caijing(财经)
    //shishang(时尚)
    //youxi(游戏)
    //qiche(汽车)
    //jiankang(健康)
    private String parseTab(String tab)
    {
        Map<String, String> map = new HashMap<>();

        map.put("头条", "top");
        map.put("国内", "guonei");
        map.put("国际", "guoji");
        map.put("娱乐", "yule");
        map.put("体育", "tiyu");
        map.put("军事", "junshi");
        map.put("科技", "keji");
        map.put("财经", "caijing");
        map.put("时尚", "shishang");
        map.put("游戏", "youxi");
        map.put("汽车", "qiche");
        map.put("健康", "jiankang");

        return map.get(tab);
    }
}
